package org.forever.util
{
	public class Stack_Array implements Stack
	{
		
		public static const CAPACITY:int = 1024;//数组的默认容量
		protected var  capacity:int;//数组的实际容量
		protected var S:Array;//对象数组
		protected var top:int = -1;//栈顶元素的位置
		
		//按默认容量创建栈对象
		public function Stack_Array()
		{	this(CAPACITY);	}
		
		//按指定容量创建栈对象
		public function Stack_Array(cap:int) {
			capacity = cap;
			S = new Object[capacity];
		}
		
		//获取栈当前的规模
		public function getSize():int
		{	return (top + 1);	}
		
		//测试栈是否为空
		public function isEmpty():Boolean
		{	return (top < 0);	}
		
		//入栈
		public function push(obj:Object):void {
			if (getSize() == capacity)
				throw new Error("意外：栈溢出");
			S[++top] = obj;
		}
		
		//取栈顶元素
		public function top():Object{
			if (isEmpty())
				throw new Error("意外：栈空");
			return S[top];
		}
		
		//出栈
		public function pop():Object {
			var elem:Object;
			if (isEmpty())
				throw new Error("意外：栈空");
			elem = S[top];
			S[top--] = null;
			return elem;
		}
	}
}